// import { title, messageDuration } from '@/config/settings';
import { setToken, getToken, removeToken } from '@/utils/auth';
import { setLocalData, getLocalData, removeLocalData } from '@/utils/localStore';

import { login } from '@/api/user';
import { hasAuthOrg } from '@/api/organization';
// import { resetRouter } from '@/router';

const initExtraLocalDataOnLogin = async userVo => {
  if (userVo.organId) {
    const res = await hasAuthOrg(userVo.organId);
    setLocalData('hasAuthOrg', res.data); // 企业认证
  }
};

const state = {
  userInfo: {},
  accessToken: '',
  hasAuthOrg: '', // 企业认证
};
const getters = {
  userInfo(state) {
    if (Object.keys(state.userInfo).length <= 0 && getToken() !== '') {
      state.userInfo = getLocalData('userInfo') || {};
    }
    return Object.assign({}, state.userInfo);
  },
  accessToken(state) {
    if (state.accessToken === '' && getToken() !== '') {
      state.accessToken = getToken();
    }
    return state.accessToken;
  },
  hasAuthOrg(state) {
    if (state.hasAuthOrg === '' && getLocalData('hasAuthOrg') !== null) {
      state.hasAuthOrg = getLocalData('hasAuthOrg');
    }
    return state.hasAuthOrg;
  },
};

const mutations = {
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken;
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  },
  setHasAuthOrg(state, hasAuthOrg) {
    state.hasAuthOrg = hasAuthOrg;
  },
};

const actions = {
  // 由页面登录设置用户和授权信息
  async login({ dispatch }, payload) {
    const { data } = await login(payload);
    const { accessToken, accessTokenExpiresIn, userVo = {} } = data;
    if (accessToken) {
      await dispatch('setAccessToken', {
        accessToken,
        accessTokenExpiresIn,
        userVo,
        isClient: false,
      });
      await initExtraLocalDataOnLogin(userVo);
    }
    return userVo;
  },
  // 由客户端设置用户和授权信息
  async setClientAuth({ dispatch }, payload) {
    const { token = {}, userInfo = {} } = payload;
    const { accessToken, accessTokenExpiresIn } = token;
    if (accessToken) {
      const [id, organId] = [userInfo.userId, userInfo.companyId];
      const userVo = { ...userInfo, id, organId, isClient: true };
      await dispatch('setAccessToken', {
        accessToken,
        accessTokenExpiresIn,
        userVo,
        isClient: true,
      });
      await initExtraLocalDataOnLogin(userVo);
    }
  },

  // 设置全部与授权和用户相关的数据
  async setAccessToken({ commit }, payload) {
    const { accessToken, accessTokenExpiresIn, userVo, isClient } = payload;
    commit('setAccessToken', accessToken);
    commit('setUserInfo', userVo);
    setToken(accessToken);
    setLocalData('userInfo', userVo, accessTokenExpiresIn);
    setLocalData('client', isClient, accessTokenExpiresIn);
  },

  async removeAccessToken({ commit }) {
    commit('setAccessToken', '');
    commit('setUserInfo', {});
    removeLocalData('userInfo');
    removeLocalData('client');
    removeToken();
  },
  async setHasAuthOrg({ commit }, payload) {
    setLocalData('hasAuthOrg', payload); // 企业认证
    commit('setHasAuthOrg', payload);
  },
};

export default { state, getters, mutations, actions };
